package com.wwdx.controller.admin;

import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxCdKeyModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.utils.ConstantHolder;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @author liguoliang
 * @date  2017/12/21
 */
@Controller
@RequestMapping("/system/wwdx_theme")
public class WwdxThemeController extends BaseController{

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView index(WwdxThemeModel model,String phase,
                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "rows",defaultValue = "10") Integer rows) {

        ModelAndView mav = new ModelAndView();
        mav.addObject("phase",phase);
        mav.addObject("listSize", wwdxThemeService.selectModelCount(model));
        mav.addObject("list", wwdxThemeService.selectModelList(model, page, rows));
        return mav;
    }

    /**
     * 书籍列表
     */
    @RequestMapping("/book_list")
    public ModelAndView themebooklist(WwdxBookModel model,
                                      @RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows) {

        ModelAndView mav = new ModelAndView();
        mav.addObject("themeid",model.getThemeid());
        mav.addObject("listSize",  wwdxBookService.selectModelCount(model));
        mav.addObject("list", wwdxBookService.selectModelList(model,page,rows));
        return mav;
    }

    /**
     * 新增
     */
    @RequestMapping("/create")
    public ModelAndView create() {
        return new ModelAndView();
    }

    /**
     * 新增保存
     */
    @RequestMapping("/save")
    public String save(WwdxThemeModel model, HttpServletRequest request) {
        this.execute(model,request);
        model.setThemeid(System.currentTimeMillis() / 1000);
        model.setGmt_created(new Date());
        model.setGmt_modified(new Date());
        wwdxThemeService.save(model);
        return "redirect:/system/wwdx_theme/list";
    }

    private void execute(WwdxThemeModel model, HttpServletRequest request){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        //主题封面 | 详情封面
        String themeImg = "ImgFile",bigThemeImg = "BigImgFile";
        fileMap.forEach((k,file)->{
            if (file.getSize() > 0) {
                if (themeImg.equalsIgnoreCase(file.getName())) {
                    model.setImg(super.upload(file, directory + ConstantHolder.Constant.COVER));
                }
                if (bigThemeImg.equalsIgnoreCase(file.getName())) {
                    model.setBigimg(super.upload(file, directory + ConstantHolder.Constant.COVER));
                }
            }
        });
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Long id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("model", wwdxThemeService.getById(id));
        return mav;
    }

    /**
     * 修改保存
     */
    @RequestMapping("/update")
    public String update(WwdxThemeModel model, HttpServletRequest request) {
        this.execute(model,request);
        model.setGmt_modified(new Date());
        wwdxThemeService.update(model);
        String key = String.format(ConstantHolder.THEME_OBJ, model.getThemeid());
        redisTemplate.delete(key);
        return "redirect:/system/wwdx_theme/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresRoles("super")
    public String delete(Long id) {
        WwdxThemeModel wwdxThemeModel = wwdxThemeService.getById(id);
        wwdxThemeService.delete(wwdxThemeModel);
        String key = String.format(ConstantHolder.THEME_OBJ, wwdxThemeModel.getThemeid());
        redisTemplate.delete(key);
        return "redirect:/system/wwdx_theme/list";
    }

    /**
     * 新增兑换码
     */
    @RequestMapping("/createCDKEY")
    public ModelAndView createCDKEY(Long themeid){
        ModelAndView mv = new ModelAndView();
        mv.addObject("themeid",themeid);
        return mv;
    }

    /**
     * 新增兑换码保存
     * @param loop 轮循次数
     */
    @RequestMapping("/addCDKEY")
    public String addCDKEY(Long themeid,Integer loop){
        WwdxCdKeyModel model = new WwdxCdKeyModel();
        model.setProduct_id(themeid);
        model.setType(1);
        wwdxCdKeyService.addCDKEY(model, loop);
        return "redirect:/system/wwdx_cdkey/list";
    }
}